<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>返回数组中指定元素的下标</title>
    <script>
        var arr = [a, a, b, b, a, b, b];
        
        function findall(arr, element) {
            var indexes = [];
            var idx = arr.indexOf(element);
            while (idx !== -1) {
                indexes.push(idx);
                idx = arr.indexOf(element, idx + 1);
            }
            return indexes;
        }

        function count(arr, element) {
            var occurrences = 0;
            var idx = arr.indexOf(element);
            while (idx !== -1) {
                occurrences++;
                idx = arr.indexOf(element, idx + 1);
            }
            return occurrences;
        }

        function displayResults() {
            var inputElement = parseInt(document.getElementById("element").value);
            var indexes = findall(arr, inputElement);
            var occurrences = count(arr, inputElement);

            document.getElementById("indexes").innerHTML = "元素 " + inputElement + " 的索引下标：" + indexes.join(", ");
            document.getElementById("occurrences").innerHTML = "元素 " + inputElement + " 出现的次数：" + occurrences;
        }
    </script>
</head>
<body>
    <input type="text" id="element" placeholder="输入要查找的元素">
    <button onclick="displayResults()">查找</button>

    <div id="indexes"></div>
    <div id="occurrences"></div>
</body>
</html>